<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <link rel="stylesheet" href="../css/weui.css"/>
    <link rel="stylesheet" href="../css/weuix.css"/>

    <script src="../js/zepto.min.js"></script>
    <script src="../js/zepto.weui.js"></script>
    <script src="../js/php.js"></script>
</head>

<body ontouchstart>
<div class="page-hd">
    <h1 class="page-hd-title">
      ajax navbar切换 无限加载
    </h1>
    <p class="page-hd-desc">特点:采用底部自动加载,分页只需要传入数据开始条数,后台不用计算数据总条数;存在bug有,从本页调到详情页,返回以后数据会重新加载,比如在拼多多跳到详情页返回也是到初始化页面,存在不能保存位置状态问题.暂时无解决好的方法,如果不是tab,可用临时缓存解决.有能解决的请提交代码</p>
</div>
<div class="weui-tab" id="t1" style="height:44px;">
    <div class="weui-navbar">
        <div class="weui-navbar__item">
            淘宝
        </div>
        <div class="weui-navbar__item">
            拼多多
        </div>
    </div>

</div>
<div class="page-bd-15">
    <div class="weui-cells" id="rank-list">
    </div>

    <div class="weui-loadmore" id="more">
        <i class="weui-loading"></i>
        <span class="weui-loadmore__tips">正在加载</span>
    </div>
</div>

<script id="tpl" type="text/html">
    <% for(var i in list) {   %>
    <div class="weui-cell hand" data-id="<%=list[i].id%>">
        <div class="weui-cell__bd">
            <p><%=list[i].id%>姓名<%=list[i].zid%>金额<%=list[i].money%></p>
        </div>
        <div class="weui-cell__ft"><%=list[i].id%></div>
    </div>
    <% } %>
</script>
<script>
    let start = 0 //开始位置
    let pagesize = 15 //每页显示
    let flag=true //是否加载完了
    let index=0 //导航默认索引
    let loading=false//防止重复加载
    function ajaxpage(start1,index1){
        $.ajax({
            url:"/zanlist1",
            data: {"start":start1,"pagesize":pagesize,"index":index1},
            type:"POST",
            dataType : "json",
            beforeSend:function(){
                $("#more").show();
            },
            success:function (rs) {
                if(rs.list==null){
                    $("#more").html("没有更多数据了");
                    return true
                }
                start = start1+count(rs.list)
                if(count(rs.list)<pagesize){
                    flag=false//设置没有数据了标记
                   $("#more").html("没有更多数据了");
                }
                $("#rank-list").append(tpl(document.getElementById('tpl').innerHTML,rs));
            }
        })

    }

    $('#t1').tab({
        // defaultIndex: 0,
        activeClass: 'tab-green',
        onToggle: function (ix) {
            start=0
            $("#rank-list").html('');
            ajaxpage(start,ix);
            flag=true
            console.log("初始化执行")
        }
    })

    $(document).on('tap click',"#rank-list .weui-cell",function(){
        id = $(this).data('id');
        location.href="js91.html?id="+id;
    })

    $(window).scroll(
        function() {
            var scrollTop = $(this).scrollTop();
            var scrollHeight = $(document).height();
            var windowHeight = $(this).height();
            if (scrollTop>scrollHeight-windowHeight-5) {
                if(loading) return;
                loading = true;
                setTimeout(function() {
                    if(flag){
                        ajaxpage(start,index)
                    }
                    loading = false;
                }, 1500);
            }
        });
</script>
<br>
<br>
<div class="weui-footer weui-footer_fixed-bottom">
    <p class="weui-footer__links">
        <a href="../index.html" class="weui-footer__link">WeUI首页</a>
    </p>
    <p class="weui-footer__text">Copyright &copy; Yoby</p>
</div>
</body>
</html>
